home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 11255 / 11255.xpi / chrome / content / controller / WindowManager.js < prev   
Text File  |  2009-11-25  |  10KB  |  295 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * 
  3.  * Pearltrees add-on AMO, Copyright(C), 2009, Broceliand SAS, Paris, France 
  4.  * (company in charge of developing Pearltrees)
  5.  * 
  6.  * This file is part of ΓÇ£Pearltrees add-on AMOΓÇ¥.  
  7.  * 
  8.  * Pearltrees add-on AMO is free software: you can redistribute it and/or modify it under the 
  9.  * terms of the GNU General Public License version 3 as published by the Free Software Foundation.
  10.  * 
  11.  * Pearltrees add-on AMO is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
  12.  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
  13.  * See the GNU General Public License for more details.
  14.  * 
  15.  * You should have received a copy of the GNU General Public License along with Pearltrees add-on AMO. 
  16.  * If not, see <http://www.gnu.org/licenses/>
  17.  * 
  18.  * ***** END LICENSE BLOCK ***** */
  19.  
  20. /////////////////////////////////////////////////////////////////////////////////
  21. // Windows manager
  22. /////////////////////////////////////////////////////////////////////////////////
  23.  
  24. const BRO_WINDOW_STORAGE_MODE_FUEL = 1; // Firefox 3+
  25. const BRO_WINDOW_STORAGE_MODE_HACK = 2; // Firefox 2-
  26.  
  27. var BRO_windowManager = {
  28.     
  29.     _storage:null,
  30.     _storageMode:null, //1='FUEL' or 2='HACK'
  31.     
  32.     init: function() {
  33.         var hiddenWindow = Components.classes["@mozilla.org/appshell/appShellService;1"]
  34.                            .getService(Components.interfaces.nsIAppShellService)
  35.                            .hiddenDOMWindow;
  36.         
  37.         // Firefox 3 use FUEL storage
  38.         if(typeof(Application) != 'undefined' && Application.storage) {
  39.             BRO_windowManager._storage = Application.storage;
  40.             BRO_windowManager._storageMode = BRO_WINDOW_STORAGE_MODE_FUEL;
  41.         }
  42.         // Firefox 2 use hidden window hack
  43.         else if(hiddenWindow) {
  44.             hiddenWindow.setBroData = function(name, value){
  45.                 this[name] = value;
  46.             }
  47.             hiddenWindow.getBroData = function(name, defaultValue){
  48.                 return (this[name])?this[name]:defaultValue;
  49.             }
  50.             BRO_windowManager._storage = hiddenWindow;
  51.             BRO_windowManager._storageMode = BRO_WINDOW_STORAGE_MODE_HACK;
  52.         }else {
  53.             BRO_log.error("Can't find a storage manager.\n We won't be able to syncronize state between windows");
  54.         }
  55.     },
  56.     
  57.     /**
  58.      * Synchronize this window with the datas stored globaly.
  59.      */
  60.     synchronize: function() {
  61.         if(!BRO_toolbar.isInit) return;
  62.         
  63.         if(BRO_model.getCurrentUser() != BRO_windowManager.getCurrentUser()) {            
  64.             BRO_model.setCurrentUser(BRO_windowManager.getCurrentUser());
  65.         }
  66.         var treeListsEquals = BRO_model.areTreeListsEquals(BRO_model.getTreeList(), BRO_windowManager.getTreeList());
  67.         var historyListsEquals = BRO_model.areHistoryListsEquals(BRO_model.getHistoryList(), BRO_windowManager.getHistoryList());
  68.         if(!treeListsEquals || !historyListsEquals) {            
  69.             BRO_model.setTreeList(BRO_windowManager.getTreeList());
  70.             BRO_model.setHistoryList(BRO_windowManager.getHistoryList());
  71.             BRO_inButtonController.rebuildTreeListAndBackupSelection();
  72.         }    
  73.         if(BRO_inButtonController.getSelectedTree() != BRO_windowManager.getSelectedTree() 
  74.            || BRO_inButtonController.getSelectedHistory() != BRO_windowManager.getSelectedHistory()
  75.            || BRO_inButtonController.getSelectedNewHistory() != BRO_windowManager.getSelectedNewHistory()) {
  76.             if(BRO_windowManager.getSelectedTree()) {                
  77.                 BRO_inButtonController.selectTree(BRO_windowManager.getSelectedTree().treeID);
  78.             }
  79.             else if(BRO_windowManager.getSelectedHistory()) {
  80.                 BRO_inButtonController.selectHistory(BRO_windowManager.getSelectedHistory().id);
  81.             }
  82.             else if(BRO_windowManager.getSelectedNewHistory()) {               
  83.                 BRO_inButtonController.selectNewHistory(BRO_windowManager.getSelectedNewHistory());
  84.             }
  85.         }
  86.         
  87.         if(BRO_toolbar.isRecording == false && BRO_windowManager.isRecording()) {
  88.             if(BRO_windowManager.isNewWindow()) {
  89.                 BRO_actionListener.setLastAction(BRO_METHOD_NEW_WINDOW);
  90.                 BRO_windowManager.setWindowOpenerID(null);
  91.             }
  92.             BRO_toolbar.setRecording(true);
  93.         }else if(BRO_toolbar.isRecording == true && !BRO_windowManager.isRecording()){
  94.             BRO_toolbar.setRecording(false);
  95.         }
  96.  
  97.         if(BRO_windowManager.getRecordingMode() && BRO_toolbar.getRecordingMode() != BRO_windowManager.getRecordingMode()) {
  98.             BRO_toolbar.setRecordingMode(BRO_windowManager.getRecordingMode());
  99.         }
  100.     },
  101.     
  102.     isNewWindow:function(){
  103.         return (BRO_windowManager.getWindowOpenerID())?true:false;
  104.     },
  105.     
  106.     /**
  107.      * This is the BrowserID of the the new window opener.
  108.      * @param string browserID
  109.      */
  110.     setWindowOpenerID:function(browserID) {
  111.         BRO_windowManager.setData('windowOpenerID',browserID);
  112.     },
  113.     
  114.     /**
  115.      * Return the BrowserID of the opener.
  116.      * @return string browserID
  117.      */
  118.     getWindowOpenerID:function() {
  119.         return BRO_windowManager.getData('windowOpenerID',null);
  120.     },
  121.     
  122.     /**
  123.      * @param boolean isRecording
  124.      */
  125.     setRecording:function(isRecording){
  126.         BRO_windowManager.setData('isRecording',isRecording);
  127.     },
  128.     
  129.     /**
  130.      * @return boolean isRecording
  131.      */
  132.     isRecording:function(){
  133.         return BRO_windowManager.getData('isRecording',false);
  134.     },
  135.     
  136.     /**
  137.     * @param integer value
  138.     */
  139.     setRecordingMode:function(value) {
  140.        BRO_windowManager.setData('recordingMode',value);
  141.     },
  142.    
  143.    /**
  144.     * @return integer
  145.     */
  146.     getRecordingMode:function() {
  147.        return BRO_windowManager.getData('recordingMode',0);
  148.     },    
  149.     
  150.     /**
  151.      * user treeList
  152.      * @param array treeList
  153.      */
  154.     setTreeList:function(treeList) {
  155.         BRO_windowManager.setData('treeList',treeList);
  156.     },
  157.     
  158.     /**
  159.      * user treeList
  160.      * @return array treeList
  161.      */
  162.     getTreeList:function() {
  163.         return BRO_windowManager.getData('treeList',null);
  164.     },
  165.     
  166.     /**
  167.     * user historyList
  168.     * @param array historyList
  169.     */
  170.    setHistoryList:function(historyList) {
  171.        BRO_windowManager.setData('historyList',historyList);
  172.    },
  173.    
  174.    /**
  175.     * user historyList
  176.     * @return array historyList
  177.     */
  178.    getHistoryList:function() {
  179.        return BRO_windowManager.getData('historyList',null);
  180.    },    
  181.     
  182.     /**
  183.     * current user
  184.     * @param currentUser
  185.     */
  186.    setCurrentUser:function(currentUser) {
  187.        BRO_windowManager.setData('currentUser',currentUser);
  188.    },
  189.    
  190.    /**
  191.     * currentUser
  192.     * @return currentUser
  193.     */
  194.    getCurrentUser:function() {
  195.        return BRO_windowManager.getData('currentUser',null);
  196.    },    
  197.     
  198.     /**
  199.      * selected tree in the list
  200.      * @param object selectedTree
  201.      */
  202.     setSelectedTree:function(selectedTree) {
  203.         BRO_windowManager.setData('selectedTree',selectedTree);
  204.     },
  205.     
  206.     /**
  207.      * selected tree in the list
  208.      * @return object
  209.      */
  210.     getSelectedTree:function() {
  211.         return BRO_windowManager.getData('selectedTree',null);
  212.     },
  213.     
  214.     /**
  215.     * selected history in the list
  216.     * @param object selectedHistory
  217.     */
  218.    setSelectedHistory:function(selectedHistory) {
  219.        BRO_windowManager.setData('selectedHistory', selectedHistory);
  220.    },
  221.    
  222.    /**
  223.     * selected history in the list
  224.     * @return object
  225.     */
  226.    getSelectedHistory:function() {
  227.        return BRO_windowManager.getData('selectedHistory',null);
  228.    },    
  229.    
  230.    /**
  231.     * newHistory
  232.     * @param string newHistory
  233.     */
  234.    setSelectedNewHistory:function(newHistory) {
  235.        BRO_windowManager.setData('newHistory', newHistory);
  236.    },
  237.    
  238.    /**
  239.     * newHistory
  240.     * @return string
  241.     */
  242.    getSelectedNewHistory:function() {
  243.        return BRO_windowManager.getData('newHistory',null);
  244.    },   
  245.     
  246.     /**
  247.      * Set data in the storage
  248.      * @param string name
  249.      * @param object value
  250.      */
  251.     setData:function(name, value) {
  252.         if(BRO_windowManager._storageMode == BRO_WINDOW_STORAGE_MODE_FUEL) {
  253.             BRO_windowManager._storage.set(name, value);
  254.         }else if(BRO_windowManager._storageMode == BRO_WINDOW_STORAGE_MODE_HACK){
  255.             BRO_windowManager._storage.setBroData(name, value);
  256.         }
  257.     },
  258.     
  259.     /**
  260.      * Get data from the storage
  261.      * @param string name
  262.      * @param object defaultValue
  263.      * @return object
  264.      */
  265.     getData:function(name, defaultValue) {
  266.         if(BRO_windowManager._storageMode == BRO_WINDOW_STORAGE_MODE_FUEL) {
  267.             return BRO_windowManager._storage.get(name, defaultValue);
  268.         }else if(BRO_windowManager._storageMode == BRO_WINDOW_STORAGE_MODE_HACK){
  269.             return BRO_windowManager._storage.getBroData(name, defaultValue);
  270.         }       
  271.     },
  272.     
  273.     /**
  274.      * Count the number of windows opened.
  275.      * @return integer
  276.      */
  277.     countWindows:function(){
  278.         if(typeof(Application) != 'undefined' && Application.windows) {
  279.             return Application.windows.length;
  280.         }else{
  281.             var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
  282.                        .getService(Components.interfaces.nsIWindowMediator);
  283.             var enumerator = wm.getEnumerator('navigator:browser');
  284.             for(var i=0; enumerator.hasMoreElements(); i++) {
  285.                 // Fix bug on reloading FF2 through chrome extension
  286.                 // nsISimpleEnumerator does not seem stable
  287.                 // TODO find a better way to do this
  288.                 if(i>50) {
  289.                     break;
  290.                 }
  291.             }
  292.             return i;
  293.         }
  294.     } 
  295. }